/**
 * Created by liubin on 2014/8/31.
 */

/**
 * 定义requirejs的总模块
 */
define(['angular', 'angular-sanitize','angular-route', 'angular-resource', 'angular-datatables',
    'angular-xeditable', 'ui-bootstrap', 'controllers', 'services', 'directives'], function (angular) {
    'use strict';
    // 设定加载的angular 模块
    return angular.module('app', [
            'ngSanitize',
            'ngRoute',
            'ngResource',
            'datatables',
            'xeditable',
            'ui.bootstrap',
            'app.controllers',
            'app.services',
            'app.directives'
        ]
    ).config(["$interpolateProvider", "$locationProvider", function ($interpolateProvider, $locationProvider) {
            $interpolateProvider.startSymbol('[[');
            $interpolateProvider.endSymbol(']]');
            // 路由 html5
            $locationProvider.html5Mode(true);
            $locationProvider.hashPrefix('!');
//            var df = angular.module('datatables.factory');
//            console.log(df);
//            df.factory('DTLoadingTemplate', function () {
//                return { html: '<h3 class="dt-loading">加载中...</h3>' };
//            });
        }]).run(["$rootScope", "$templateCache", "$interpolate", "$DTDefaultOptions",'editableOptions',"pageSize",
            function ($rootScope, $templateCache, $interpolate, $DTDefaultOptions, editableOptions, pageSize) {
            $rootScope.$on("$routeChangeSuccess", function (scope, current) {
                var url = current.templateUrl ? current.templateUrl : current.redirectTo;
                console.log(url);
//                switch (url) {
//                    case urlRoute.getUrl('tpl_userlist'):
//                    case urlRoute.getUrl('tpl_useredit'):
//                        $rootScope.menu = {
//                            userlist: 'active'
//                        }
//                        break;
//                    case urlRoute.getUrl('tpl_rolelist'):
//                        $rootScope.menu = {
//                            rolelist: 'active'
//                        }
//                        break;
//                    case urlRoute.getUrl('index'):
//                        $rootScope.menu = {};
//                        break;
//                }
                // 把模板里面的 angularjs 标准的 {{ }} 绑定符号替换成 自定义的替换符号,目前使用 [[ ]]
                var getFn = $templateCache.get;
                $templateCache.get = function (key) {
                    var result = getFn(key);
                    console.log(result);
                    if(result && !Array.isArray(result)){
                        //return result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol);
                        console.log(result.replace(/{{/g, $interpolate.startSymbol));
                        console.log(result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol));
                        console.log($interpolate.startSymbol);
                        console.log($interpolate.endSymbol);
                        return result.replace(/{{/g, $interpolate.startSymbol).replace(/}}/g, $interpolate.endSymbol);
                    }
                    else{
                        return result;
                    }
                }
                $DTDefaultOptions.setLanguage({
                    sLengthMenu: '每页显示 _MENU_ 条记录',
                    sZeroRecords: '0条记录',
                    sInfo: '第 _PAGE_ 页，共 _PAGES_ 页',
                    sInfoEmpty: '没有数据可以显示',
                    sEmptyTable: '表格为空',
                    sInfoFiltered: '(总共 _MAX_ 条记录中过滤)',
                    sSearch: '检索',
                    oPaginate: {
                        sFirst : '首页',
                        sPrevious : '上一页',
                        sNext : '下一页',
                        sLast : '末页'
                    }
                });
                $DTDefaultOptions.setDisplayLength(pageSize);
                editableOptions.theme = 'bs3';

                // 加载自定义模板
                $templateCache.put('template/app/spinModal.html',
                '<div class="modal-body">\n' +
                    '<h4 class="smaller lighter grey">\n' +
                    '<i class="ace-icon fa fa-spinner fa-spin orange bigger-125"></i>\n' +
                    '处理中\n' +
                    '</h4>\n'+
                    '</div>\n' +
                    "");

                $templateCache.put('template/app/confirmModal.html',
                '<div class="modal-header">\n' +
                    '<h3 class="modal-title" ng-bind="title"></h3>\n' +
                    '</div>\n' +
                    '<div class="modal-body">\n' +
                    '<h4 ng-bind="content" class="text-center"></h4>\n' +
                    '</div>\n' +
                    '<div class="modal-footer">\n' +
                    '<button class="btn btn-primary" type="button" ng-click="ok()">确定</button>\n' +
                    '<button class="btn btn-danger" type="button" ng-click="cancel()">取消</button>\n' +
                    '</div>\n' +
                    "");
                $templateCache.put('template/app/alertModal.html',
                '<div class="modal-header">\n' +
                    '<h3 class="modal-title" ng-bind="title"></h3>\n' +
                    '</div>\n' +
                    '<div class="modal-body">\n' +
                    '<h4 ng-bind="content" class="text-center"></h4>\n' +
                    '</div>\n' +
                    '<div class="modal-footer">\n' +
                    '<button class="btn btn-primary" type="button" ng-click="ok()">确定</button>\n' +
                    '</div>\n' +
                    "");
            });
        }]);
});